var app = getApp();
Page({

  data: {
	  cdnUrl: '',
    userInfo: null,
		/* 自定义导航栏参数 */
		loading: false,
		color: '#FFFFFF',
		background: '#FFFFFF',
		show: true,
		animated: false,
		back: true,
		title: "设置"
  },

  onLoad: function (options) {
		var that = this;
    that.setData({
      cdnUrl: app.cdnUrl,
      userInfo: app.globalData.userInfo
    });
  },

  onShow: function () {
		var that = this;
		that.setData({
			cdnUrl: app.cdnUrl,
		});
    if (!app.globalData.userInfo) {
      app.error("请登录后再操作", function () {
        setTimeout(function () { wx.navigateBack({}) }, 2000);
      });
    }
		that.setData({
		  userInfo: app.globalData.userInfo
		});
  },

  formSubmit: function (event) {
    var that = this;
    if (event.detail.value.username == '') {
      app.error('用户名不能为空');
      return;
    }
    app.request('/addons/cms/wxapp.user/profile', event.detail.value, function (data) {
      that.setData({ userInfo: data.userInfo });
      app.globalData.userInfo = data.userInfo;
      app.success('修改成功!', function () {
        setTimeout(function () {
          //要延时执行的代码
          wx.reLaunch({
            url: '/page/setting/index'
          });
        }, 2000); //延迟时间
      });
    }, function (data, ret) {
      app.error(ret.msg);
    });
  },
	logout: function (){
    var that = this;
    app.request('/addons/cms/wxapp.user/logout', {}, function (data) {
      that.setData({ userInfo: null });
      app.globalData.userInfo = null;
      app.success('成功退出!');
      setTimeout(function () {
        //要延时执行的代码
        wx.reLaunch({
          url: '/page/index/index'
        });
      }, 1500); //延迟时间
    }, function (data, ret) {
      app.error(ret.msg);
    });
  //   console.log(123);
		// app.globalData.userInfo = null;
		// this.setData({
		// 	userInfo: null
		// })
		// app.success('成功退出!', function () {
		//   setTimeout(function () {
		//     //要延时执行的代码
		//     wx.reLaunch({
		//       url: '/page/index/index'
		//     });
		//   }, 2000); //延迟时间
		// });
		// this.onShow();
	},
  //上传头像
  uploadAvatar: function () {
    if (!app.globalData.userInfo) {
      app.error("请登录后再操作");
      return false;
    }
    var that = this;
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        var formData = app.globalData.config.upload.multipart;
        formData.token = app.globalData.userInfo.token;
        wx.uploadFile({
          url: app.globalData.config.upload.uploadurl,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: formData,
          success: function (res) {          
            if(res.statusCode != 200){
              app.error(res.errMsg);
              return;
            }
            var row = JSON.parse(res.data);
            if (row.code == 1) {
              app.request("/addons/cms/wxapp.user/avatar", { avatar: row.data.url }, function (data, ret) {
                app.success('头像上传成功!');
                app.globalData.userInfo = data.userInfo;
                that.setData({ userInfo: data.userInfo});
								that.setData({ ["userInfo.avatar"]: app.globalData.config.upload.cdnurl + row.data.url });
								wx.navigateBack({});
              }, function (data, ret) {
                app.error(ret.msg);
              });
            }
          }, error: function (res) {
            app.error("上传头像失败!");
          }
        });
      }, error: function (res) {
        app.error("上传头像失败!");
      }
    });
  },

})